﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.IO;

public partial class BasicBase_Vehicle_VehicleList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            init();

        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindGrid(PageIndex);
    }

    void init()
    {
        if (Request.QueryString["Where"] != null)
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200607"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            top.Visible = false;
            ListPageContral1.SetFirstPage();
            BindGrid(0);
        }
        else
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200610") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200611") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200603"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }


            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }

            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200611"))
            {
                DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
                DepartmentList.Enabled = false;
                DepartmentListBind();
            }
            else

                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200603"))
                {
                    DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
                    DepartmentList.Enabled = false;
                    DepartmentListBind();
                    UnitList.SelectedValue = department.DepartmentID;
                    UnitList.Enabled = false;
                }

            //车辆类型


            ddlType.DataTextField = "ItemName";
            ddlType.DataValueField = "ItemID";
            ddlType.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and ParentID='10' and ItemID<>'1099' order by SeqID,ItemID ");
            ddlType.DataBind();
            ddlClass.DataTextField = "ItemName";
            ddlClass.DataValueField = "ItemID";
            BindClass();
            ListPageContral1.SetFirstPage();
            BindGrid(0);
        }
    }

    void DepartmentListBind()
    {
        if (DepartmentList.SelectedValue == "")
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("------------部门-------------", ""));
            UnitList.DataSource = null;
            UnitList.DataBind();
        }
        else
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("------------部门-------------", ""));
            string strWhere = "(departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or departmentid='" + DepartmentList.SelectedValue + "'";
            UnitList.DataSource = Department.GetList(strWhere);
            UnitList.DataBind();
        }
    }

    private void GetWhere()
    {
        if (Request.QueryString["Where"] != null)
        {
            lblWhere.Text = Request.QueryString["Where"];
            UserGrid.Columns[0].Visible = false;
        }
        else
        {
            string where = "";
            if (UnitList.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                where += " DepartmentID='" + UnitList.SelectedValue + "' ";
            }
            else
            {
                if (DepartmentList.SelectedValue == "")
                {
                    UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                    DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
                    if (userInfo.DepartmentID.Substring(0, 4) != "1300")
                    {
                        where += where == "" ? "" : " And ";
                        where += " DepartmentID like '" + userInfo.DepartmentID.Substring(0, 4) + "%' ";
                    }
                }
                else
                {
                    where += where == "" ? "" : " And ";
                    where += " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
                }
            }
            //
            if (ddlType.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                if (ddlClass.SelectedValue == "")
                {
                    where += " VehicleClass like '" + ddlType.Text + "__' ";
                }
                else
                {
                    where += " VehicleClass='" + ddlClass.SelectedValue + "' ";
                }
            }
            lblWhere.Text = where;
        }
    }

    public string GetCodeItem(string GroupID, string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass, GroupID);
        return info != null ? info.ItemName : "";
    }

    //随车装备
    protected void ButtonDevices_Click(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200605"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                Response.Redirect("VehicleDevices.aspx?id=" + UserGrid.DataKeys[i].Values[0].ToString());
                break;
            }
        }
        Prompt.Text = "警告：请选择记录";
    }

    #region 通用代码
    protected void ButtonEdit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                Response.Redirect("VehicleNew.aspx?id=" + UserGrid.DataKeys[i].Values[0].ToString());
                break;
            }
        }
        Prompt.Text = "警告：请选择记录";
    }
    protected void ButtonDelete_Click(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200604"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        int affectedRows = 0;
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "删除记录";
                log.ItemID = UserGrid.DataKeys[i].Values[0].ToString();
                log.ItemName = "Devices";
                log.ItemFullUrl = "DeviceList.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
                string pp = UserGrid.DataKeys[i].Values[0].ToString();
                //删除附件
                List<AttachmentInfo> AttachmentList = Attachment.GetList("ItemID='" + pp + "'");
                foreach (AttachmentInfo info in AttachmentList)
                {
                    string Attfile = Server.MapPath("../../" + info.FilePath);
                    if (File.Exists(Attfile))
                    {
                        FileInfo file = new FileInfo(Attfile);
                        //file.IsReadOnly = false;
                        file.Delete();
                    }
                    Attachment.Delete(info.FileID);
                }
                //图片删除
                Guid id = new Guid(UserGrid.DataKeys[i].Values[0].ToString());
                string fileName = Vehicle.GetDetail(id).Picture;
                if (fileName != "")
                {

                    System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("pics\\") + fileName);
                    if (file.Exists)
                    {
                        file.IsReadOnly = false;
                        file.Delete();
                    }
                }
                Vehicle.Delete(new Guid(pp));

                affectedRows++;

            }
        }
        ListPageContral1.SetFirstPage();
        BindGrid(0);
        Prompt.Text = "提示：共删除" + affectedRows.ToString() + "条记录！";
    }


    /// <summary>
    /// 绑定GridView
    /// </summary>
    private void BindGrid(int pageIndex)
    {
        GetWhere();
        List<VehicleInfo> list = Vehicle.GetList(ListPageContral1.PageSize, pageIndex, lblWhere.Text);
        ListPageContral1.ItemCount = Vehicle.GetCount(lblWhere.Text);
        UserGrid.DataSource = list;
        UserGrid.DataBind();

    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    #endregion

    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
        Search();
    }

    private void BindClass()
    {
        ddlClass.Items.Clear();
        ddlClass.Items.Add(new ListItem("---类别---", ""));
        if (ddlType.SelectedValue == "")
        {
            ddlClass.DataSource = "";
            ddlClass.DataBind();
        }
        else
        {
            ddlClass.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and  parentID='" + ddlType.SelectedValue + "' order by ItemID,SeqID ");
            ddlClass.DataBind();
        }
    }
    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        Search();
    }

    //查询梆定
    private void Search()
    {

        ListPageContral1.SetFirstPage();
        BindGrid(0);
    }

    protected string GetDepartmentName(string data)
    {
        return Department.GetDepartment(data).FullName;
    }
    protected void ButtonAttachment_Click(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200608"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (!IsClientScriptBlockRegistered("OpenScript"))
                {
                    RegisterStartupScript("OpenScript", "<script>OpenWin('../../Common/Upload.aspx?ItemID=" + UserGrid.DataKeys[i].Values[0].ToString() + "');</script>");
                }
                //Response.Redirect("../../Common/Upload.aspx?ItemID=" + UserGrid.DataKeys[i].Values[0].ToString());
            }
        }
        Prompt.Text = "警告：请选择记录";
    }
    protected void Record_Click(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200609"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (!IsClientScriptBlockRegistered("OpenScript"))
                {
                    RegisterStartupScript("OpenScript", "<script>OpenWin('../../Common/LogList.aspx?ItemID=" + UserGrid.DataKeys[i].Values[0].ToString() + "');</script>");
                }
                //Response.Redirect("../../Common/LogList.aspx?ItemID=" + UserGrid.DataKeys[i].Values[0].ToString());
            }
        }
        Prompt.Text = "警告：请选择记录";
    }
    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
        Search();
    }
    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Search();
    }

    public string GetType(string itemID)
    {
        CodeItemInfo Item = CodeItem.GetCodeItem(itemID, "XFZBLB");
        CodeItemInfo Pitem = CodeItem.GetCodeItem(Item.ParentID, "XFZBLB");
        return Pitem.ItemName;
    }

}
